package com.link.base.base.product.service;

import com.alibaba.excel.EasyExcel;
import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.product.dao.mybatis.mapper.PcdDataMapper;
import com.link.base.base.product.model.PcdData;
import com.link.base.base.product.model.PcdDataCheckListener;
import com.link.base.base.product.model.PcdDataExcelMo;
import com.link.base.base.product.model.PcdDataSaveListener;
import com.link.base.base.project.model.Project;
import com.link.base.base.project.service.ProjectService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.UserUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * pcd对象
 *
 * @date 2023-08-15 09:04:14
 */
@Service
public class PcdDataServiceImpl extends BasicServiceImpl<PcdData> implements PcdDataService {


    @Resource
    private PcdDataMapper    PcdDataMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private PcdDataMapper    pcdDataMapper;

    @Resource
    private PcdDataService pcdDataService;

    @Resource
    private ProjectService projectService;

    @Override
    public BasicMapper<PcdData> getBasicMapper() {
        return PcdDataMapper;
    }

    @Override
    public void uploadExcelAnalysis(MultipartFile multipartFile,Long headId) throws Exception {
        // 校验项目是否存在
        Project project = projectService.queryById(headId);
        if (Objects.isNull(project)) {
            throw new BasicServiceException("项目不存在");
        }
        // TODO 实现Excel数据上传并解析，并插入到PcdDataMapper中
        // 注意：此处需要引入Excel相关的依赖和jar包
        // 请自行实现Excel的读取和解析
        // 示例代码：
        // 这里默认读取第一个sheet
        // 校验数据
        EasyExcel.read(multipartFile.getInputStream(), PcdDataExcelMo.class, new PcdDataCheckListener())
                .sheet()
                .headRowNumber(7)
                .doRead();
        // 保存数据
        EasyExcel.read(multipartFile.getInputStream(), PcdDataExcelMo.class, new PcdDataSaveListener(this.pcdDataService,headId))
                .sheet()
                .headRowNumber(7)
                .doRead();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertExcels(List<PcdDataExcelMo> list,Long headId) throws Exception {
        for (PcdDataExcelMo pcdDataExcelMo : list) {
            PcdData pcdDataNew = new PcdData();
            pcdDataNew.setProjectId(headId);
            pcdDataNew.setId(keyGenerateService.keyGenerate());
            pcdDataNew.setCreatedBy(UserUtil.getUser().getId());
            pcdDataNew.setLastUpdatedBy(UserUtil.getUser().getId());
            pcdDataNew.setOrgId(UserUtil.getUser().getOrgId());
            pcdDataNew.setCorpid(UserUtil.getUser().getLoginCorpId());
            pcdDataNew.setPostnId(UserUtil.getUser().getPostnId());
            BeanUtils.copyProperties(pcdDataNew,pcdDataExcelMo);
            pcdDataMapper.importInsertData(pcdDataNew);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchImportData(List<PcdDataExcelMo> list,Long headId) {
        try {
            pcdDataService.batchInsertExcels(list, headId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
